﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Client1
{
    public partial class spiritOfVikingForm : Form
    {
        bool mouse_down = false;
        GameMaster myGameMaster = new GameMaster();
        int oldX = 0, oldY = 0;

        public spiritOfVikingForm()
        {
            InitializeComponent();
        }

        private void spiritOfVikingForm_Load(object sender, EventArgs e)
        {
            //upload background image
            Bitmap myImage = new Bitmap(@"spirit_of_viking\cartoon-ocean-005.jpg");
            myPictureBox.Image = myImage;
            enemyPictureBox.Image = myImage;
        }

        private void myPictureBox_Click(object sender, EventArgs e)
        {

        }

        private void myPictureBox_Paint(object sender, PaintEventArgs e)
        {
            //draw grid by drawing a lot of rectangle
            //28 is the size of each square
            for (int d = 0; d < 10; d++)
            {
                for (int f = 0; f < 10; f++)
                {
                    e.Graphics.DrawRectangle(new Pen(Color.Black, 2f), new Rectangle((28 * d), (28 * f), 28, 28));
                }
            }
            myGameMaster.draw(e);
        }

        private void enemyPictureBox_Paint(object sender, PaintEventArgs e)
        {
            //draw grid by drawing a lot of rectangle
            //28 is the size of each square
            for (int d = 0; d < 10; d++)
            {
                for (int f = 0; f < 10; f++)
                {
                    e.Graphics.DrawRectangle(new Pen(Color.Black, 2f), new Rectangle((28 * d), (28 * f), 28, 28));
                }
            }
        }

        private void myPictureBox_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mouse_down = true;
                myGameMaster.findShip(e.Location.X, e.Location.Y);
                oldX = e.X; oldY = e.Y;
                Console.WriteLine("old x coordinate: " + oldX.ToString() + ", old y coordinate: " + oldY.ToString());
            }
            else if (e.Button == MouseButtons.Right)
            {

            }
        }

        private void myPictureBox_MouseMove(object sender, MouseEventArgs e)
        {
            if (mouse_down == true)
            {
                myGameMaster.moveShip(e.X, e.Y);
                myPictureBox.Refresh();
            }
        }

        private void myPictureBox_MouseUp(object sender, MouseEventArgs e)
        {
            myGameMaster.releaseShip(e.X, e.Y);
            mouse_down = false;
            myPictureBox.Refresh();
        }

        /// <summary>
        /// change coordinate from 28,54 to 1,B
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="xValue"></param>
        /// <param name="yValue"></param>
        private static void setCoordinate(int x, int y, ref int xValue, ref char yValue)
        {
            int ytemp = 0;

            for (int d = 0; d < 11; d++)
            {
                if (x > 28 * d)
                    xValue = d + 1;
                if (y > 28 * d)
                    ytemp = d + 1;
            }

            while (ytemp > 0)
            {
                yValue++; ytemp--;
            }
        }
    }
}
